//index.js
//获取应用实例
const app = getApp()
const sss = require('../../son/SHA-1/sha.js');
const totp = require('../../son/SHA-1/totp.js');

var head_left_judge = false;
var touch_start_point = null;
var touch_end_point = null;
var box_true_array = [true, false, false];
var check_out_who = 0;
let cache_jud = false;
var num = 0;
var num_jud = true;
var charts_need_array = [];
var weekNum1 = 0;

Page({
    data: {
        colorArrays: ["#FBCF6D", "#C8C8F7", "#FEBCD9", "#5FE1D1", "#8BD3F9", "#64E1B1", "#12AEF3", "#E29AAD"],
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),

        username: '',
        pwd: '',
        pwd2: '',
        pwd3: '',
        sid: "",

        animation1: {},
        mask_fade: {},
        head_left_in: {},
        mask_hide: false,
        hid_jie: true,

        box_true: [true, false, false],
    
        wifi_status:"未知"
    },
    //事件处理函数

    head_left_tap: function () {
        this.onLoad();
    },
    onPullDownRefresh: function () {
        this.onShow();
    },

    onLoad: function () {


    },

    onReady: function () {
      
    },
    onShow: function () {
        //掌上武大Tim
        wx.getStorage({
            key: 'stdNumber',
            success: function (res) {
                if (res.data.length != 13) {
                    wx.showToast({
                        title: '账号有误',
                        duration: 1500
                    })
                }
            },
            fail: function () {
                wx.showToast({
                    title: '请登入',
                    duration: 1500
                })
                wx.switchTab({
                    url: 'index(1)'
                })
            }
        })

        var _this = this;
        if (app.globalData.sid != null) {
            _this.setData({
                sid: app.globalData.sid,
            })
        }
        else {
            _this.setData({
                sid: "尚未登入",
            })
        }//

        //-----数据获取
        if (app.globalData.term_now_year != null) {
            var _this = this;
            wx.getStorage({
                key: 'wifiNumber',
                success: function (res) {
                    let cache = res.data;
                    app.globalData.pwd_wifi = cache;
                    wx.getStorage({
                        key: 'stdNumber',
                        success: function (res) {
                            let cache = res.data;
                            if (cache.length == 13) {
                                app.globalData.sid = cache;
                            }
                            wx.getStorage({
                                key: 'eduNumber',
                                success: function (res) {
                                    let cache = res.data;
                                    app.globalData.pwd = cache;
                                    wx.getStorage({
                                        key: 'libNumber',
                                        success: function (res) {

                                            if (app.globalData.sid != null && app.globalData.sid.length == 13) {
                                                _this.setData({
                                                    sid: app.globalData.sid,
                                                })
                                            }
                                            else {
                                                _this.setData({
                                                    sid: "尚未登入",
                                                })
                                            }//页面初始化账户信息


                                            let cache = res.data;
                                            app.globalData.pwd_lib = cache;

                                            //数据的获取
                                            var sid = app.globalData.sid;
                                            var pwd_edu = app.globalData.pwd;
                                            var pwd_lib = app.globalData.pwd_lib;
                                            var pwd_wifi = app.globalData.pwd_wifi;

                                            var a =
                                                wx.request({
                                                    url: 'https://api.ziqiang.net.cn/token/', //仅为示例，并非真实的接口地址
                                                    method: "GET",
                                                    data: {
                                                        sid: sid,
                                                        pwd_edu: pwd_edu,
                                                        pwd_lib: pwd_lib,
                                                        pwd_wifi: pwd_wifi,
                                                        binding_type: 2
                                                    },
                                                    dataType: JSON,
                                                    header: {
                                                        'content-type': 'application/json' // 默认值
                                                    },
                                                    success: function (res) {
                                                        _this.setData({
                                                            isChecked: false,
                                                        })

                                                        wx.showToast({
                                                            title: '获取wifi状态',
                                                            icon: 'success',
                                                            duration: 2000,
                                                        })
                                                        _this.setData({
                                                            isAccount: true,
                                                        })

                                                        var data = JSON.parse(res.data);

                                                        app.globalData.Token = data.token;
                                                        console.log(JSON.parse(res.data));
                                                        var totpSECRET_KEY = sss.getEncodeString('+.a*vD@JrLYauz(M)HA^=}sh');
                                                        var secret_key = totp.updateOtp(totpSECRET_KEY);
                                                        //totp.getCode(totpSECRET_KEY)
                                                        var timestamp = Math.round(new Date().getTime() / 1000);
                                                        var timestampstr = timestamp.toString();//获取unix时间戳字符串
                                                        var ran = Math.ceil(Math.random() * 1000000000);
                                                        var ranstr = ran.toString();
                                                        var arr = new Array(3);
                                                        arr[0] = secret_key;
                                                        arr[1] = timestampstr;
                                                        arr[2] = ranstr;
                                                        arr = arr.sort();//字典序排序
                                                        var mix = arr[0] + arr[1] + arr[2];
                                                        var sha1 = sss.de2(mix).toString();//sha1算法
                                                        var a =
                                                            wx.request({
                                                                url: 'https://api.ziqiang.net.cn/wifi/status/', //仅为示例，并非真实的接口地址
                                                                method: "GET",
                                                                data: {

                                                                },
                                                                dataType: JSON,
                                                                header: {
                                                                    'content-type': 'application/json', // 默认值
                                                                    'Authorization': 'Token ' + app.globalData.Token,
                                                                    'x-zswd-signature': sha1,
                                                                    'x-zswd-timestamp': timestamp,
                                                                    'x-zswd-nonce': ran,
                                                                },
                                                                success: function (res) {
                                                                    var data=JSON.parse(res.data);
                                                                    if (data.status=="resume"){
                                                                      _this.setData({
                                                                        wifi_status:"正常使用中"
                                                                      })
                                                                    }
                                                                    else if (data.status == "suspend")                                                                        {
                                                                        _this.setData({
                                                                            wifi_status: "暂停中"
                                                                        })
                                                                    }
                                                                    else{
                                                                        _this.setData({
                                                                            wifi_status: "未知"
                                                                        })
                                                                    }
                                                                    if(res.statusCode!=200){
                                                                        _this.onShow();
                                                                    }
                                                                },
                                                                Error: function (res) {
                                                                    _this.onShow();
                                                                    wx.showToast({
                                                                        title: '登入失败',
                                                                        icon: 'cancel',
                                                                        duration: 2000,
                                                                    })
                                                                }
                                                            })
                                                        

                                                        /*var f =
                                                          wx.request({
                                                            url: 'https://api.ziqiang.net.cn/courses/grade', //仅为示例，并非真实的接口地址
                                                            method: "GET",
                                                            data: {
                                                              "instructor":"李美幸",
                                                            },
                                                            dataType: JSON,
                                                            header: {
                                                              'content-type': 'application/json', // 默认值
                                                              'Authorization': 'Token ' + app.globalData.Token,
                                                              'x-zswd-signature': sha1,
                                                              'x-zswd-timestamp': timestamp,
                                                              'x-zswd-nonce': ran,
                                                            },
                                                            success: function (res) {
                                  
                                                              var data = JSON.parse(res.data);
                                                             
                                  
                                                            },
                                                            Error: function (res) {
                                                              wx.showToast({
                                                                title: '登入失败',
                                                                icon: 'cancel',
                                                                duration: 2000,
                                                              })
                                                            }
                                                          })*/
                                                    },
                                                    Error: function (res) {
                                                        wx.showToast({
                                                            title: '登入失败',
                                                            icon: 'cancel',
                                                            duration: 2000,
                                                        })
                                                    }
                                                })
                                        },

                                    })//三部获取图书馆密码
                                },
                            })//二步获取

                        },
                        fail: function () {
                            _this.setData({
                                sid: '.尚未登入.',
                            })
                        },

                    })//一步获取账号
                }
            })//四步获取wifi密码
            //各种数据的获取



            if (app.globalData.userInfo) {
                this.setData({
                    userInfo: app.globalData.userInfo,
                    hasUserInfo: true
                })
            } else if (this.data.canIUse) {
                // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                // 所以此处加入 callback 以防止这种情况
                app.userInfoReadyCallback = res => {
                    this.setData({
                        userInfo: res.userInfo,
                        hasUserInfo: true
                    })
                }
            } else {
                // 在没有 open-type=getUserInfo 版本的兼容处理
                wx.getUserInfo({
                    success: res => {
                        app.globalData.userInfo = res.userInfo
                        this.setData({
                            userInfo: res.userInfo,
                            hasUserInfo: true
                        })
                    }
                })
            }//网络请求

        }
    },


    onReady: function () {

    },
 
    switchChange:function(e){
        console.log(e);
        if(e.detail.value==true){
            wx.getStorage({
                key: 'stdNumber',
                success: function (res) {
                    if (res.data.length != 13) {
                        wx.showToast({
                            title: '账号有误',
                            duration: 1500
                        })
                    }
                },
                fail: function () {
                    wx.showToast({
                        title: '请登入',
                        duration: 1500
                    })
                    wx.switchTab({
                        url: 'index(1)'
                    })
                }
            })

            var _this = this;
            if (app.globalData.sid != null) {
                _this.setData({
                    sid: app.globalData.sid,
                })
            }
            else {
                _this.setData({
                    sid: "尚未登入",
                })
            }//

            //-----数据获取
            if (app.globalData.term_now_year != null) {
                var _this = this;
                wx.getStorage({
                    key: 'wifiNumber',
                    success: function (res) {
                        let cache = res.data;
                        app.globalData.pwd_wifi = cache;
                        wx.getStorage({
                            key: 'stdNumber',
                            success: function (res) {
                                let cache = res.data;
                                if (cache.length == 13) {
                                    app.globalData.sid = cache;
                                }
                                wx.getStorage({
                                    key: 'eduNumber',
                                    success: function (res) {
                                        let cache = res.data;
                                        app.globalData.pwd = cache;
                                        wx.getStorage({
                                            key: 'libNumber',
                                            success: function (res) {

                                                if (app.globalData.sid != null && app.globalData.sid.length == 13) {
                                                    _this.setData({
                                                        sid: app.globalData.sid,
                                                    })
                                                }
                                                else {
                                                    _this.setData({
                                                        sid: "尚未登入",
                                                    })
                                                }//页面初始化账户信息


                                                let cache = res.data;
                                                app.globalData.pwd_lib = cache;

                                                //数据的获取
                                                var sid = app.globalData.sid;
                                                var pwd_edu = app.globalData.pwd;
                                                var pwd_lib = app.globalData.pwd_lib;
                                                var pwd_wifi = app.globalData.pwd_wifi;

                                                var a =
                                                    wx.request({
                                                        url: 'https://api.ziqiang.net.cn/token/', //仅为示例，并非真实的接口地址
                                                        method: "GET",
                                                        data: {
                                                            sid: sid,
                                                            pwd_edu: pwd_edu,
                                                            pwd_lib: pwd_lib,
                                                            pwd_wifi: pwd_wifi,
                                                            binding_type: 2
                                                        },
                                                        dataType: JSON,
                                                        header: {
                                                            'content-type': 'application/json' // 默认值
                                                        },
                                                        success: function (res) {
                                                            _this.setData({
                                                                isChecked: false,
                                                            })

                                                            
                                                            _this.setData({
                                                                isAccount: true,
                                                            })

                                                            var data = JSON.parse(res.data);

                                                            app.globalData.Token = data.token;
                                                            console.log(JSON.parse(res.data));
                                                            var totpSECRET_KEY = sss.getEncodeString('+.a*vD@JrLYauz(M)HA^=}sh');
                                                            var secret_key = totp.updateOtp(totpSECRET_KEY);
                                                            //totp.getCode(totpSECRET_KEY)
                                                            var timestamp = Math.round(new Date().getTime() / 1000);
                                                            var timestampstr = timestamp.toString();//获取unix时间戳字符串
                                                            var ran = Math.ceil(Math.random() * 1000000000);
                                                            var ranstr = ran.toString();
                                                            var arr = new Array(3);
                                                            arr[0] = secret_key;
                                                            arr[1] = timestampstr;
                                                            arr[2] = ranstr;
                                                            arr = arr.sort();//字典序排序
                                                            var mix = arr[0] + arr[1] + arr[2];
                                                            var sha1 = sss.de2(mix).toString();//sha1算法
                                                            var a =
                                                                wx.request({
                                                                    url: 'https://api.ziqiang.net.cn/wifi/switch/', //仅为示例，并非真实的接口地址
                                                                    method: "GET",
                                                                    data: {
                                                                        status:1
                                                                    },
                                                                    dataType: JSON,
                                                                    header: {
                                                                        'content-type': 'application/json', // 默认值
                                                                        'Authorization': 'Token ' + app.globalData.Token,
                                                                        'x-zswd-signature': sha1,
                                                                        'x-zswd-timestamp': timestamp,
                                                                        'x-zswd-nonce': ran,
                                                                    },
                                                                    success: function (res) {
                                                                        wx.showToast({
                                                                            title: 'wifi状态切换',
                                                                            icon: 'success',
                                                                            duration: 2000,
                                                                        })
                                                                        var data = JSON.parse(res.data);
                                                                        if (data.status == "resume") {
                                                                            _this.setData({
                                                                                wifi_status: "正常使用中"
                                                                            })
                                                                        }
                                                                        else if (data.status == "suspend") {
                                                                            _this.setData({
                                                                                wifi_status: "暂停中"
                                                                            })
                                                                        }
                                                                        else {
                                                                            _this.setData({
                                                                                wifi_status: "未知"
                                                                            })
                                                                        }
                                                                        if (res.statusCode != 200) {
                                                                            _this.onShow();
                                                                        }
                                                                    },
                                                                    Error: function (res) {
                                                                        _this.onShow();
                                                                        wx.showToast({
                                                                            title: '登入失败',
                                                                            icon: 'cancel',
                                                                            duration: 2000,
                                                                        })
                                                                    }
                                                                })

                                                        },
                                                        Error: function (res) {
                                                            wx.showToast({
                                                                title: '登入失败',
                                                                icon: 'cancel',
                                                                duration: 2000,
                                                            })
                                                        }
                                                    })
                                            },

                                        })//三部获取图书馆密码
                                    },
                                })//二步获取

                            },
                            fail: function () {
                                _this.setData({
                                    sid: '.尚未登入.',
                                })
                            },

                        })//一步获取账号
                    }
                })//四步获取wifi密码
                //各种数据的获取

            }
        }
        else if (e.detail.value == false) {
            wx.getStorage({
                key: 'stdNumber',
                success: function (res) {
                    if (res.data.length != 13) {
                        wx.showToast({
                            title: '账号有误',
                            duration: 1500
                        })
                    }
                },
                fail: function () {
                    wx.showToast({
                        title: '请登入',
                        duration: 1500
                    })
                    wx.switchTab({
                        url: 'index(1)'
                    })
                }
            })

            var _this = this;
            if (app.globalData.sid != null) {
                _this.setData({
                    sid: app.globalData.sid,
                })
            }
            else {
                _this.setData({
                    sid: "尚未登入",
                })
            }//

            //-----数据获取
            if (app.globalData.term_now_year != null) {
                var _this = this;
                wx.getStorage({
                    key: 'wifiNumber',
                    success: function (res) {
                        let cache = res.data;
                        app.globalData.pwd_wifi = cache;
                        wx.getStorage({
                            key: 'stdNumber',
                            success: function (res) {
                                let cache = res.data;
                                if (cache.length == 13) {
                                    app.globalData.sid = cache;
                                }
                                wx.getStorage({
                                    key: 'eduNumber',
                                    success: function (res) {
                                        let cache = res.data;
                                        app.globalData.pwd = cache;
                                        wx.getStorage({
                                            key: 'libNumber',
                                            success: function (res) {

                                                if (app.globalData.sid != null && app.globalData.sid.length == 13) {
                                                    _this.setData({
                                                        sid: app.globalData.sid,
                                                    })
                                                }
                                                else {
                                                    _this.setData({
                                                        sid: "尚未登入",
                                                    })
                                                }//页面初始化账户信息


                                                let cache = res.data;
                                                app.globalData.pwd_lib = cache;

                                                //数据的获取
                                                var sid = app.globalData.sid;
                                                var pwd_edu = app.globalData.pwd;
                                                var pwd_lib = app.globalData.pwd_lib;
                                                var pwd_wifi = app.globalData.pwd_wifi;

                                                var a =
                                                    wx.request({
                                                        url: 'https://api.ziqiang.net.cn/token/', //仅为示例，并非真实的接口地址
                                                        method: "GET",
                                                        data: {
                                                            sid: sid,
                                                            pwd_edu: pwd_edu,
                                                            pwd_lib: pwd_lib,
                                                            pwd_wifi: pwd_wifi,
                                                            binding_type: 2
                                                        },
                                                        dataType: JSON,
                                                        header: {
                                                            'content-type': 'application/json' // 默认值
                                                        },
                                                        success: function (res) {
                                                            _this.setData({
                                                                isChecked: false,
                                                            })

                                                            wx.showToast({
                                                                title: '获取wifi状态',
                                                                icon: 'success',
                                                                duration: 2000,
                                                            })
                                                            _this.setData({
                                                                isAccount: true,
                                                            })

                                                            var data = JSON.parse(res.data);

                                                            app.globalData.Token = data.token;
                                                            console.log(JSON.parse(res.data));
                                                            var totpSECRET_KEY = sss.getEncodeString('+.a*vD@JrLYauz(M)HA^=}sh');
                                                            var secret_key = totp.updateOtp(totpSECRET_KEY);
                                                            //totp.getCode(totpSECRET_KEY)
                                                            var timestamp = Math.round(new Date().getTime() / 1000);
                                                            var timestampstr = timestamp.toString();//获取unix时间戳字符串
                                                            var ran = Math.ceil(Math.random() * 1000000000);
                                                            var ranstr = ran.toString();
                                                            var arr = new Array(3);
                                                            arr[0] = secret_key;
                                                            arr[1] = timestampstr;
                                                            arr[2] = ranstr;
                                                            arr = arr.sort();//字典序排序
                                                            var mix = arr[0] + arr[1] + arr[2];
                                                            var sha1 = sss.de2(mix).toString();//sha1算法
                                                            var a =
                                                                wx.request({
                                                                    url: 'https://api.ziqiang.net.cn/wifi/switch/', //仅为示例，并非真实的接口地址
                                                                    method: "GET",
                                                                    data: {
                                                                        status: 0
                                                                    },
                                                                    dataType: JSON,
                                                                    header: {
                                                                        'content-type': 'application/json', // 默认值
                                                                        'Authorization': 'Token ' + app.globalData.Token,
                                                                        'x-zswd-signature': sha1,
                                                                        'x-zswd-timestamp': timestamp,
                                                                        'x-zswd-nonce': ran,
                                                                    },
                                                                    success: function (res) {
                                                                        var data = JSON.parse(res.data);
                                                                        if (data.status == "resume") {
                                                                            _this.setData({
                                                                                wifi_status: "正常使用中"
                                                                            })
                                                                        }
                                                                        else if (data.status == "suspend") {
                                                                            _this.setData({
                                                                                wifi_status: "暂停中"
                                                                            })
                                                                        }
                                                                        else {
                                                                            _this.setData({
                                                                                wifi_status: "未知"
                                                                            })
                                                                        }
                                                                        if (res.statusCode != 200) {
                                                                            _this.onShow();
                                                                        }
                                                                    },
                                                                    Error: function (res) {
                                                                        _this.onShow();
                                                                        wx.showToast({
                                                                            title: '登入失败',
                                                                            icon: 'cancel',
                                                                            duration: 2000,
                                                                        })
                                                                    }
                                                                })

                                                        },
                                                        Error: function (res) {
                                                            wx.showToast({
                                                                title: '登入失败',
                                                                icon: 'cancel',
                                                                duration: 2000,
                                                            })
                                                        }
                                                    })
                                            },

                                        })//三部获取图书馆密码
                                    },
                                })//二步获取

                            },
                            fail: function () {
                                _this.setData({
                                    sid: '.尚未登入.',
                                })
                            },

                        })//一步获取账号
                    }
                })//四步获取wifi密码
                //各种数据的获取

            }
        }
    },
    back: function () {
        wx.navigateBack({
            url: '/pages/index/index'
        })
    },


})

